﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

namespace sgsBase
{
	/// <summary>
	/// Base file for Global.asax file providing extenders into the system.
	/// </summary>
	public class SgsGlobal : System.Web.HttpApplication
	{
		private string m_NextPage = "";
		private string m_focusField = "";
		private bool m_alldata = false;
		private string m_parent = "";
		private string m_page = "";
		//private SgsUser _user = new SgsUser();

		public SgsUser CurUser
		{
			
			get
			{
				SgsUser tUser;
				try
				{
					if (Session["User"] == null)
					{
						tUser = new SgsUser();
						Session["User"] = tUser;
					}
					else
						tUser = (SgsUser)Session["User"];
				}
				catch (Exception)
				{
					tUser = new SgsUser();
					Session["User"] = tUser;
				}

				return tUser; 
			}
			//set { tUser = value; }
		}

		public string PageName
		{
			get
			{
				if (m_page == "")
				{
					m_page = Request.Path.ToLower();
					if (ParentFolder != "")
						m_page = m_page.Replace(ParentFolder, "");
				}
				return m_page;
			}
		}

		public string ParentFolder
		{
			get
			{
				if (m_parent == "")
				{
					//trying to determine if there is a parent directory or if this is the root.
					// if the last part of the phsyical path is the same as the first of the http
					// path, then that is the 'root' directory for the pages.
					string sub = Path.GetDirectoryName(Request.Path.ToLower()).Trim('\\');
					if (sub == "")
						return "";
					string phys = Path.GetDirectoryName(ApplicationHelper.PhysicalPath.ToLower()).Trim('\\');

					string[] ra = sub.Split('\\');
					string[] ra2 = phys.Split('\\');
					if (ra[0] == ra2[ra2.Length - 1])
						m_parent = "/" + ra[0];
				}
				return m_parent;
			}
		}

		public bool HasSubFolder
		{
			get
			{
				string sub = Request.Path.ToLower();
				sub = Path.GetDirectoryName(sub).Trim('\\');
				string t = ParentFolder;
				if (t != "")
					sub = sub.Replace(t.Substring(1), "");
				if (sub != "")
				{
					return true;
				}
				return false;
			}
		}

		public string Message
		{
			get
			{
				if (Session["Message"] == null)
				{
					return "";
				}
				return Session["Message"].ToString();
			}
			set
			{
				Session["Message"] = value;
			}
		}

		public bool AllDataComplete
		{
			get { return m_alldata; }
			set { m_alldata = value; }

		}
		public string NextPage
		{
			get { return m_NextPage; }
			set { m_NextPage = value; }
		}

		public string focusField
		{
			get { return m_focusField; }
			set { m_focusField = value; }
		}
		
		protected void Application_BeginRequest(object sender, EventArgs e)
		{
			m_page = "";
			m_parent = "";
		}
	// end class
	}
}
